package com.example.taobaounion.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.taobaounion.R;
import com.example.taobaounion.utils.LogUtils;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    private State currentState=State.NONE;
    private View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;

    //枚举类
    public enum State{
        NONE,LOADING,SUCCESS,ERROR,EMPTY
    }

    private Unbinder mBind;
    private FrameLayout mBaseContainer;

    @Override
    public View onCreateView(@NonNull  LayoutInflater inflater, @Nullable  ViewGroup container, @Nullable  Bundle savedInstanceState) {
        //给状态们加个坑放各个状态页面
        View rootView = loadRootView(inflater,container);
        mBaseContainer = rootView.findViewById(R.id.base_container);
        //加载状态View
        loadStatesView(inflater,container);
        //这个坑mBaseContainer在加载状态view的时候已经把成功的view,失败的view,加载的view添加进来了，
        // 所以ButterKnife的时候的rootView就都可以访问到我们需要三个View的id
        mBind = ButterKnife.bind(this, rootView);
        LogUtils.d(this,"rootView"+rootView.toString());
        //初始化View
        initView(rootView);
        //设置监听事件
        initListener();
        //初始化实现类
        initPresenter();
        //加载数据
        loadData();
        return rootView;
    }

    /**
     * 初始化监听
     * 如果子类需要去设置相关的监听事件，就可以覆盖此方法
     */
    protected void initListener() {

    }

    //默认情况加载这个view(也就是填充整个页面)，特殊情况，子类去复写（如果需要改布局）
    protected View loadRootView(LayoutInflater inflater,ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }

    /**
     * 加载各种状态的View
     * @param inflater
     * @param container
     */
    private void loadStatesView(LayoutInflater inflater, ViewGroup container) {
        //成功的view
        mSuccessView = loadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccessView);
        //loading的view
        mLoadingView = loadingView(inflater, container);
        mBaseContainer.addView(mLoadingView);
        //错误页面
        mErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mErrorView);
        //内容为空的页面
        mEmptyView = loadEmptyView(inflater, container);
        mBaseContainer.addView(mEmptyView);
        //一开始都默认什么都没有为NONE
        setUpState(State.NONE);
    }

    @OnClick(R.id.network_error_tips)
    public void retry(){
        //点击了重新加载内容
        LogUtils.d(this,"点击了网络重新加载");
        onRetryClick();
    }

    /**
     * 如果子fragment需要加载网络错误以后的点击，覆盖某些方法即可
     */
    protected void onRetryClick() {

    }

    protected View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_error,container,false);
    }


    protected View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty,container,false);
    }

    //暴露方法切换状态
    /**
     * 子类通过这个方法来切换页面状态
     * @param state
     */
    public void setUpState(State state){
        this.currentState=state;
        mSuccessView.setVisibility(currentState==State.SUCCESS?View.VISIBLE:View.GONE);
        mLoadingView.setVisibility(currentState==State.LOADING?View.VISIBLE:View.GONE);
        mErrorView.setVisibility(currentState==State.ERROR?View.VISIBLE:View.GONE);
        mEmptyView.setVisibility(currentState==State.EMPTY?View.VISIBLE:View.GONE);
    }

    protected View loadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading,container,false);
    }

    protected void initView(View rootView) {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mBind!=null){
            mBind.unbind();
        }
        release();
    }

    protected void release() {
        //释放资源
    }

    //不一定所有都要，所以使用protected
    protected void loadData() {
        //加载数据
    }

    //初始化实现类
    protected void initPresenter() {

    }

    //真正加载本身Fragment的页面
    protected View loadSuccessView(LayoutInflater inflater, ViewGroup container) {
        int resId=getRootViewResId();
        return inflater.inflate(resId,container,false);
    }
    //使用protected表示继承BaseFragment的子类可以访问到这个方法,才能让其子类实现该方法
    protected abstract int getRootViewResId();


}
